//
//  WordListDetailViewController.swift
//  ShimaPlayer
//
//  Created by michael on 2025/7/10.
//

import UIKit

class WordListDetailViewController: UIViewController {
    
    private lazy var backButton: UIButton = {
        let button = UIButton(type: .system)
        button.setImage(
            UIImage(named: "nav_back_gray")?.withRenderingMode(.alwaysOriginal), for: .normal)
        button.addTarget(self, action: #selector(backButtonTapped), for: .touchUpInside)
        return button
    }()

    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = "词单详情"
        label.textColor = UIColor(hexString: "FFFFFF")
        label.font = .systemFont(ofSize: 17, weight: .medium)
        label.textAlignment = .center
        return label
    }()
    
    private lazy var coverView: ListCoverView = {
        let view = ListCoverView()
        view.onClick = { [weak self] in
            self?.showCoverPicker()
        }
        return view
    }()

    private var wordListNameLabel: UILabel = {
        let label = UILabel()
        label.text = "250307T1新编日语第2册词单"
        label.textColor = UIColor(hexString: "FFFFFF")
        label.font = .systemFont(ofSize: 16, weight: .bold)
        return label
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    private func setupUI() {
        navigationController?.navigationBar.isHidden = true
        
        let bgImage = UIImage.gradientImage(colors: [UIColor(hexString: "#1B2340"), UIColor(hexString: "#1C2341"), UIColor(hexString: "#363F65")],
                                            size: view.bounds.size,
                                            startPoint: CGPoint(x: 0.25, y: 0.5),
                                            endPoint: CGPoint(x: 0.75, y: 0.5),
                                            locations: [0, 0.15, 0.46],
                                            transform: CATransform3DMakeAffineTransform(CGAffineTransform(a: 1.2, b: 1.24, c: -1.24, d: 0.25, tx: 0.51, ty: -0.36))
        )
        let bgView = UIImageView(image: bgImage)
        view.addSubview(bgView)
        bgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

        view.addSubview(backButton)
        backButton.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(10)
            make.leading.equalToSuperview().offset(16)
            make.height.equalTo(44)
            make.width.equalTo(20)
        }

        view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalTo(backButton)
            make.centerX.equalToSuperview()
        }

        view.addSubview(coverView)
        coverView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(37)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(154)
        }

        let editIcon = UIImageView(image: UIImage(named: "btn_icon_edit"))
        editIcon.contentMode = .scaleAspectFit
        let wordListNameView = TipsView(elements: [wordListNameLabel, editIcon]) { [weak self] in
            self?.onTapEditWordListName()
        }.hiddenBg()
        view.addSubview(wordListNameView)
        wordListNameView.snp.makeConstraints { make in
            make.top.equalTo(coverView.snp.bottom).offset(18)
            make.centerX.equalToSuperview()
            make.height.equalTo(35)
        }
    }

    // MARK: - Actions
    @objc private func backButtonTapped() {
        navigationController?.popViewController(animated: true)
    }
    
    private func showCoverPicker() {
//        let picker = UIImagePickerController()
//        picker.delegate = self
//        picker.sourceType = .photoLibrary
//        present(picker, animated: true, completion: nil)
    }
    
    private func onTapEditWordListName() {
        
    }

}
